from django.db import models

from user.models import User


# 题目表
class Problem(models.Model):
    EASY = 'easy'
    MEDIUM = 'medium'
    HARD = 'hard'
    LEVEL_CHOICES = [
        (EASY, '简单'),
        (MEDIUM, '中等'),
        (HARD, '困难'),
    ]

    title_en = models.CharField(verbose_name = "题目标题（英文）", max_length = 64, null = False)
    title_cn = models.CharField(verbose_name = "题目标题（中文）", max_length = 64, null = False)
    level = models.CharField(verbose_name = "题目难度", max_length = 16, choices = LEVEL_CHOICES,
                             null = False)
    description = models.TextField(verbose_name = "题目描述", default = "暂无描述")
    
    # header = models.TextField(verbose_name = "题目头部（展示给用户的预留代码）", null = False)
    # tail = models.TextField(verbose_name = "题目尾部（后端测试代码）", null = False)
    
    cpu_limit = models.IntegerField(verbose_name = "时间限制（ms）", null = False)
    mem_limit = models.IntegerField(verbose_name = "空间限制（MB）", null = False)
    tags = models.ManyToManyField(verbose_name = "题目标签", to = 'ProblemTag', related_name = 'problems')
    passes = models.IntegerField(verbose_name = "题目通过数", default = 0)

    class Meta:
        db_table = 'problem'
        
        
# 题目头部表（预留代码）
class ProblemHeader(models.Model):
    problem = models.ForeignKey(Problem, verbose_name = "所属题目", on_delete = models.CASCADE)
    cpp_header = models.TextField(verbose_name = "C++题目头部", null = False)
    java_header = models.TextField(verbose_name = "Java题目头部", null = False)
    python3_header = models.TextField(verbose_name = "Python3题目头部", null = False)
  
  
# 题目头部表（测试代码）
class ProblemTail(models.Model):
    problem = models.ForeignKey(Problem, verbose_name = "所属题目", on_delete = models.CASCADE)
    cpp_tail = models.TextField(verbose_name = "C++题目尾部", null = False)
    java_tail = models.TextField(verbose_name = "Java题目尾部", null = False)
    python3_tail = models.TextField(verbose_name = "Python3题目尾部", null = False)


# 题目标签
class ProblemTag(models.Model):
    tag_en = models.CharField(verbose_name = "标签名（英文）", max_length = 64, unique = True)
    tag_cn = models.CharField(verbose_name = "标签名（中文）", max_length = 64, unique = True)

    class Meta:
        db_table = 'problem_tag'

    def __str__(self):
        return self.tag_cn


# 题目评论
class Comment(models.Model):
    user = models.ForeignKey(User, verbose_name = "评论用户", on_delete = models.CASCADE)
    problem = models.ForeignKey(Problem, verbose_name = "所属题目", on_delete = models.CASCADE)
    content = models.TextField()
    create_datetime = models.DateTimeField(verbose_name = '评论时间', auto_now_add = True, )


# 提交记录
class Submission(models.Model):
    problem = models.ForeignKey(Problem, on_delete = models.CASCADE, verbose_name = "提交题目")
    user = models.ForeignKey(User, on_delete = models.CASCADE, verbose_name = "提交用户")
    submit_datetime = models.DateTimeField(auto_now_add = True, verbose_name = "提交时间")
    status = models.CharField(max_length = 32, verbose_name = "提交状态")
    submit_code = models.TextField(verbose_name = "提交代码")
    status_detail = models.TextField(verbose_name = "状态描述")
    use_time = models.CharField(max_length=32,verbose_name="运行时间")
    use_memory = models.CharField(max_length=32,verbose_name="运行内存")
    language = models.CharField(max_length=32,verbose_name="编程语言")

    class Meta:
        ordering = ['-submit_datetime']
